﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;
    using System.Xml;

    public partial class frmCapNhatDmGhiChuCachDung : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmGhiChuCachDungEvent;
        int State = 0;
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmGhiChuCachDungBussiness dmGhiChuCachDungBussiness = new DmGhiChuCachDungBussiness();
        DmGhiChuCachDungEntity dmGhiChuCachDungEntity = null;

        public frmCapNhatDmGhiChuCachDung( int state)
        {
            InitializeComponent();
            State = state;
        }

        public frmCapNhatDmGhiChuCachDung(int state, DmGhiChuCachDungEntity obj)
        {
            InitializeComponent();

            State = state;
            dmGhiChuCachDungEntity = obj;

            txtMaGhiChu.Text = dmGhiChuCachDungEntity.MaGhiChuCachDung;
            txtTenGhiChu.Text = dmGhiChuCachDungEntity.TenGhiChuCachDung;
            txtMoTa.Text = dmGhiChuCachDungEntity.MoTaGhiChu;
            chkTrangThaiSuDung.Checked =dmGhiChuCachDungEntity.TrangThaiSuDung;
            txtMaGhiChu.Enabled = false;
            txtTenGhiChu.Focus();
        }

        private void frmcapnhatDMGhichuCachdung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaGhiChu, "Mã ghi chú cách dùng") == false) return;
            
            dmGhiChuCachDungEntity = new DmGhiChuCachDungEntity()
            {
                MaGhiChuCachDung=txtMaGhiChu.Text.Trim(),
                TenGhiChuCachDung=txtTenGhiChu.Text.Trim(),
                MoTaGhiChu=txtMoTa.Text.Trim(),
                TrangThaiSuDung=(bool) chkTrangThaiSuDung.Checked 
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmGhiChuCachDungBussiness.GetDmGhiChuCachDung(txtMaGhiChu.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaGhiChu, "Mã ghi chú cách dùng") == false) return;
                dmGhiChuCachDungBussiness.InsertDmGhiChuCachDung(dmGhiChuCachDungEntity);
                Validation.ClearControl(this);
                txtMaGhiChu.Focus();
            }
            else
            {
                dmGhiChuCachDungBussiness.EditDmGhiChuCachDung(dmGhiChuCachDungEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmGhiChuCachDungEvent != null)
                dmGhiChuCachDungEvent(sender, e);
        }

    }
}